package cc.bukkitPlugin.banitem.command.analyse;

import java.util.ArrayList;
import java.util.Arrays;

import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.command.CommandSender;

import cc.bukkitPlugin.banitem.BanItem;
import cc.bukkitPlugin.banitem.CFG;
import cc.bukkitPlugin.banitem.check.SimpleWorld;
import cc.bukkitPlugin.banitem.command.AParamCommand;

public class ParamWorld extends AParam{

    public ParamWorld(BanItem pPlugin,AnalyseMan pAnalyor){
        super(pPlugin,pAnalyor);
        this.mLabels=new String[]{"w","world"};
    }

    @Override
    public AnalyseResult handle(CommandSender pSender,AnalyseResult pCmdParam,String pArg){
        World tWorld=Bukkit.getWorld(pArg);
        if(tWorld==null)
            return pCmdParam.buildError("世界["+pArg+"]不存在");
        pCmdParam.mWorld=SimpleWorld.create(tWorld.getName());
        return pCmdParam.buildSuccess();
    }

    @Override
    public ArrayList<String> tabComplete(CommandSender pSender,AParamCommand pCommand,String[] pArgs){
        ArrayList<String> tWorldsStr=new ArrayList<>();
        for(World sWorld : Bukkit.getWorlds())
            tWorldsStr.add(sWorld.getName());
        if(pArgs==null||pArgs.length==0)
            return tWorldsStr;
        String[] params=pArgs[pArgs.length-1].split(":",2);
        String tLabel=params[0]+":";
        String part="";
        if(params.length==2)
            part=params[1].toLowerCase();
        else return tWorldsStr;
        ArrayList<String> fitWorldList=new ArrayList<>();
        for(String sKey : tWorldsStr){
            if(sKey.toLowerCase().startsWith(part)){
                fitWorldList.add(tLabel+sKey);
                if(fitWorldList.size()>=100)
                    return fitWorldList;
            }
        }
        return fitWorldList;
    }

    @Override
    public ArrayList<String> getHelp(){
        ArrayList<String> help=new ArrayList<>();
        help.add(CFG.cmd_color+"  "+Arrays.toString(this.mLabels));
        help.add(CFG.help_color+"    设置操作的世界");
        help.add(CFG.help_color+"    如果未设置,则为默认世界配置");
        return help;
    }

}
